package com.jian.supernote.bao;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.AppCompatTextView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.CompoundButton;

import com.jian.supernote.R;

public class DialogUtils {
    private static Dialog dialog;

    /**
     * 取消弹窗
     */
    public static void dismiss() {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
            dialog = null;
        }
    }

    private static void showCenter(Context context, View view) {
        dismiss();
        dialog = getInstance(context);
        dialog.setCancelable(false);
        dialog.setContentView(view);
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        DisplayMetrics display = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(display);
        params.width = (int) (0.85d * display.widthPixels);
        dialog.getWindow().setAttributes(params);
        dialog.show();
    }

    private static Dialog getInstance(Context context) {
        if (dialog == null) {
            dialog = new Dialog(context, R.style.DialogStyle);
        }
        return dialog;
    }

    public static void showDoubleBtnDialog(Context context, String title, String message,
                                           String message2, View.OnClickListener msg2ClickListener,
                                           String leftText, View.OnClickListener leftClickListener,
                                           String rightText, View.OnClickListener rightClickListener) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_base_double_btn_dialog, null);
        AppCompatTextView tvTitle = view.findViewById(R.id.tv_dialog_title);
        AppCompatTextView tvMsg = view.findViewById(R.id.tv_dialog_msg);
        AppCompatCheckBox cbApprove = view.findViewById(R.id.cb_approve);
        AppCompatTextView tvMsg2 = view.findViewById(R.id.tv_dialog_msg_2);
        AppCompatButton btnLeft = view.findViewById(R.id.btn_dialog_left);
        final AppCompatButton btnRight = view.findViewById(R.id.btn_dialog_right);
        tvTitle.setText(title);
        tvMsg.setText(message);
        tvMsg2.setText(message2);
        tvMsg2.setOnClickListener(msg2ClickListener);
        btnLeft.setText(leftText);
        btnLeft.setOnClickListener(leftClickListener);
        btnRight.setText(rightText);
        btnRight.setOnClickListener(rightClickListener);
        btnRight.setEnabled(false);
        cbApprove.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                btnRight.setEnabled(isChecked);
            }
        });
        showCenter(context, view);
    }
}
